home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / chrome / toolkit.jar / content / global / commonDialog.js < prev    next >
Encoding:
JavaScript  |  2005-08-20  |  10.9 KB  |  334 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Mozilla Communicator client code.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Netscape Communications Corporation.
  18.  * Portions created by the Initial Developer are Copyright (C) 1998
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Alec Flett  <alecf@netscape.com>
  23.  *   Ben Goodger <ben@netscape.com>
  24.  *   Blake Ross  <blakeross@telocity.com>
  25.  *   Joe Hewitt <hewitt@netscape.com>
  26.  *
  27.  * Alternatively, the contents of this file may be used under the terms of
  28.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  29.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30.  * in which case the provisions of the GPL or the LGPL are applicable instead
  31.  * of those above. If you wish to allow use of your version of this file only
  32.  * under the terms of either the GPL or the LGPL, and not to allow others to
  33.  * use your version of this file under the terms of the MPL, indicate your
  34.  * decision by deleting the provisions above and replace them with the notice
  35.  * and other provisions required by the GPL or the LGPL. If you do not delete
  36.  * the provisions above, a recipient may use your version of this file under
  37.  * the terms of any one of the MPL, the GPL or the LGPL.
  38.  *
  39.  * ***** END LICENSE BLOCK ***** */
  40.  
  41. // parameters to gCommonDialogParam.Get() are defined in nsPIPromptService.idl
  42. var gCommonDialogParam = 
  43.   window.arguments[0].QueryInterface(Components.interfaces.nsIDialogParamBlock);
  44.   
  45. function showControls()
  46. {
  47.   // This is called before onload fires, so we can't be certain that any elements
  48.   // in the document have their bindings ready, so don't call any methods/properties
  49.   // here on xul elements that come from xbl bindings.
  50.   
  51.   // show the required textboxes and set their initial values
  52.   var nTextBoxes = gCommonDialogParam.GetInt(3);
  53.   if (nTextBoxes == 2) {
  54.     if (gCommonDialogParam.GetInt(4) == 1) {
  55.       initTextbox("password1", 4, 6, false);
  56.       initTextbox("password2", 5, 7, false);
  57.     }
  58.     else {
  59.       initTextbox("login", 4, 6, false);
  60.       initTextbox("password1", 5, 7, false);
  61.     }
  62.   } else if (nTextBoxes == 1) {
  63.     if (gCommonDialogParam.GetInt(4) == 1)
  64.       initTextbox("password1", -1, 6, true);
  65.     else
  66.       initTextbox("login", 4, 6, true);
  67.   }
  68. }
  69.  
  70. function setLabelForNode(aNode, aLabel, aIsLabelFlag)
  71. {
  72.   // This is for labels with possible accesskeys
  73.   var accessKeyIndex;
  74.   if (/^\&[^\&]/.test(aLabel)) { // access key is at the start
  75.    accessKeyIndex = 0;
  76.   } else {
  77.     accessKeyIndex = aLabel.search(/[^\&]\&[^\&]/) + 1;
  78.     if (accessKeyIndex == 0) {
  79.       accessKeyIndex = -1; // magic value for no accesskey
  80.     }
  81.   }
  82.  
  83.   // If a character has an & before it, then it should become an accesskey
  84.   if (accessKeyIndex >= 0 && accessKeyIndex < aLabel.length - 1) {
  85.     // This will also cause the accesskey attribute to be set via xbl
  86.     aNode.accessKey = aLabel.charAt(accessKeyIndex + 1);
  87.     // Set the label to the string without the &
  88.     aLabel = aLabel.substr(0, accessKeyIndex) + 
  89.              aLabel.substr(accessKeyIndex + 1);
  90.   }
  91.   aLabel = aLabel.replace(/\&\&/g, "&");
  92.   if (aIsLabelFlag) {    // Set text for <label> element
  93.     aNode.setAttribute("value", aLabel);
  94.   } else {    // Set text for other xul elements
  95.     aNode.label = aLabel;
  96.   }
  97. }
  98.  
  99. function commonDialogOnLoad()
  100. {
  101.   // set the document title
  102. //@line 105 "/c/mozilla/toolkit/content/commonDialog.js"
  103.   document.title = gCommonDialogParam.GetString(12);
  104. //@line 107 "/c/mozilla/toolkit/content/commonDialog.js"
  105.  
  106.   // set the number of command buttons
  107.   var nButtons = gCommonDialogParam.GetInt(2);
  108.   var dialog = document.documentElement;
  109.   switch (nButtons) {
  110.     case 1:
  111.       dialog.getButton("cancel").hidden = true;
  112.       break;
  113.     case 4:
  114.       dialog.getButton("extra2").hidden = false;
  115.     case 3:
  116.       dialog.getButton("extra1").hidden = false;
  117.   }
  118.  
  119.   // display the main text
  120.   var messageText = gCommonDialogParam.GetString(0);
  121.   var messageParent = document.getElementById("info.box");
  122.   var messageParagraphs = messageText.split("\n");
  123.  
  124.   for (var i = 0; i < messageParagraphs.length; i++) {
  125.     var descriptionNode = document.createElement("description");
  126.     var text = document.createTextNode(messageParagraphs[i]);
  127.     descriptionNode.appendChild(text);
  128.     messageParent.appendChild(descriptionNode);
  129.   }
  130.  
  131.   setElementText("info.header", gCommonDialogParam.GetString(3), true);
  132.  
  133.   // set the icon
  134.   var iconElement = document.getElementById("info.icon");
  135.   var iconClass = gCommonDialogParam.GetString(2);
  136.   if (!iconClass)
  137.     iconClass = "message-icon";
  138.   iconElement.setAttribute("class", iconElement.getAttribute("class") + " " + iconClass);
  139.  
  140.   switch (nButtons) {
  141.     case 4:
  142.       setLabelForNode(document.documentElement.getButton("extra2"), gCommonDialogParam.GetString(11));
  143.       // fall through
  144.     case 3:
  145.       setLabelForNode(document.documentElement.getButton("extra1"), gCommonDialogParam.GetString(10));
  146.       // fall through
  147.     default:
  148.     case 2:
  149.       var string = gCommonDialogParam.GetString(9);
  150.       if (string)
  151.         setLabelForNode(document.documentElement.getButton("cancel"), string);
  152.       // fall through
  153.     case 1:
  154.       string = gCommonDialogParam.GetString(8);
  155.       if (string)
  156.         setLabelForNode(document.documentElement.getButton("accept"), string);
  157.       break;
  158.   }
  159.  
  160.   // set default result to cancelled
  161.   gCommonDialogParam.SetInt(0, 1); 
  162.  
  163.   // initialize the checkbox
  164.   setCheckbox(gCommonDialogParam.GetString(1), gCommonDialogParam.GetInt(1));
  165.  
  166.   if (gCommonDialogParam.GetInt(3) == 0) // If no text fields
  167.   {
  168.     var dlgButtons = ['accept', 'cancel', 'extra1', 'extra2'];
  169.  
  170.     // Set the default button and focus it
  171.     var dButton = dlgButtons[gCommonDialogParam.GetInt(5)];
  172.     document.documentElement.defaultButton = dButton;
  173.     document.documentElement.getButton(dButton).focus();
  174.   }
  175.  
  176.   if (gCommonDialogParam.GetInt(6) != 0) // delay button enable
  177.   {
  178.     var delayInterval = 2000;
  179.     try {
  180.       var prefs = Components.classes["@mozilla.org/preferences-service;1"]
  181.                   .getService(Components.interfaces.nsIPrefBranch);
  182.       delayInterval = prefs.getIntPref("security.dialog_enable_delay");
  183.     } catch (e) {}
  184.  
  185.     document.documentElement.getButton("accept").disabled = true;
  186.     document.documentElement.getButton("extra1").disabled = true;
  187.     document.documentElement.getButton("extra2").disabled = true;
  188.  
  189.     setTimeout(commonDialogReenableButtons, delayInterval);
  190.     
  191.     addEventListener("blur", commonDialogBlur, false);
  192.     addEventListener("focus", commonDialogFocus, false);
  193.   }
  194.  
  195.   getAttention();
  196. }
  197.  
  198. var gDelayExpired = false;
  199. var gBlurred = false;
  200.  
  201. function commonDialogBlur(aEvent)
  202. {
  203.   if (aEvent.target != document)
  204.     return;
  205.   gBlurred = true;
  206.   document.documentElement.getButton("accept").disabled = true;
  207.   document.documentElement.getButton("extra1").disabled = true;
  208.   document.documentElement.getButton("extra2").disabled = true;
  209. }
  210.  
  211. function commonDialogFocus(aEvent)
  212. {
  213.   if (aEvent.target != document)
  214.     return;
  215.   gBlurred = false;
  216.   // When refocusing the window, don't enable the buttons unless the countdown
  217.   // delay has expired. 
  218.   if (gDelayExpired) {
  219.     var script = "document.documentElement.getButton('accept').disabled = false; ";
  220.     script += "document.documentElement.getButton('extra1').disabled = false; ";
  221.     script += "document.documentElement.getButton('extra2').disabled = false;";
  222.     setTimeout(script, 250);
  223.   }
  224. }
  225.  
  226. function commonDialogReenableButtons()
  227. {
  228.   // Don't automatically enable the buttons if we're not in the foreground
  229.   if (!gBlurred) {
  230.     document.documentElement.getButton("accept").disabled = false;
  231.     document.documentElement.getButton("extra1").disabled = false;
  232.     document.documentElement.getButton("extra2").disabled = false;
  233.   }
  234.   gDelayExpired = true;
  235. }
  236.  
  237. function initTextbox(aName, aLabelIndex, aValueIndex, aAlwaysLabel)
  238. {
  239.   unHideElementById(aName+"Container");
  240.  
  241.   var label = aLabelIndex < 0 ? "" : gCommonDialogParam.GetString(aLabelIndex);
  242.   if (label || aAlwaysLabel && !label)
  243.     setElementText(aName+"Label", label);
  244.     
  245.   var value = aValueIndex < 0 ? "" : gCommonDialogParam.GetString(aValueIndex);
  246.   var textbox = document.getElementById(aName + "Textbox");
  247.   textbox.setAttribute("value", value);
  248. }
  249.  
  250. function setElementText(aElementID, aValue, aChildNodeFlag)
  251. {
  252.   var element = document.getElementById(aElementID);
  253.   if (!aChildNodeFlag && element) {
  254.     setLabelForNode(element, aValue, true);
  255.   } else if (aChildNodeFlag && element) {
  256.     element.appendChild(document.createTextNode(aValue));
  257.   }
  258. }
  259.  
  260. function setCheckbox(aChkMsg, aChkValue)
  261. {
  262.   if (aChkMsg) {
  263.     // XXX Would love to use hidden instead of collapsed, but the checkbox
  264.     // fails to size itself properly when I do this.
  265.     document.getElementById("checkboxContainer").removeAttribute("collapsed");
  266.     
  267.     var checkboxElement = document.getElementById("checkbox");
  268.     setLabelForNode(checkboxElement, aChkMsg);
  269.     checkboxElement.checked = aChkValue > 0;
  270.   }
  271. }
  272.  
  273. function unHideElementById(aElementID)
  274. {
  275.   var element = document.getElementById(aElementID);
  276.   element.hidden = false;
  277. }
  278.  
  279. function hideElementById(aElementID)
  280. {
  281.   var element = document.getElementById(aElementID)
  282.   element.hidden = true;
  283. }
  284.  
  285. function isVisible(aElementId)
  286. {
  287.   return document.getElementById(aElementId).hasAttribute("hidden");
  288. }
  289.  
  290. function onCheckboxClick(aCheckboxElement)
  291. {
  292.   gCommonDialogParam.SetInt(1, aCheckboxElement.checked);
  293. }
  294.  
  295. function commonDialogOnAccept()
  296. {
  297.   gCommonDialogParam.SetInt(0, 0); // say that ok was pressed
  298.  
  299.   var numTextBoxes = gCommonDialogParam.GetInt(3);
  300.   var textboxIsPassword1 = gCommonDialogParam.GetInt(4) == 1;
  301.   
  302.   if (numTextBoxes >= 1) {
  303.     var editField1;
  304.     if (textboxIsPassword1)
  305.       editField1 = document.getElementById("password1Textbox");
  306.     else
  307.       editField1 = document.getElementById("loginTextbox");
  308.     gCommonDialogParam.SetString(6, editField1.value);
  309.   }
  310.  
  311.   if (numTextBoxes == 2) {
  312.     var editField2;
  313.     if (textboxIsPassword1)
  314.       // we had two password fields
  315.       editField2 = document.getElementById("password2Textbox");
  316.     else
  317.       // we only had one password field (and one login field)
  318.       editField2 = document.getElementById("password1Textbox");
  319.     gCommonDialogParam.SetString(7, editField2.value);
  320.   }
  321. }
  322.  
  323. function commonDialogOnExtra1()
  324. {
  325.   gCommonDialogParam.SetInt(0, 2);
  326.   window.close();
  327. }
  328.  
  329. function commonDialogOnExtra2()
  330. {
  331.   gCommonDialogParam.SetInt(0, 3);
  332.   window.close();
  333. }
  334.